package com.yunbiao.ybbusstopplate.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.ProxyInfo;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;

import com.google.android.material.tabs.TabLayout;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class NetController {

    private static String TAG = "NetController";

    /**
     * 主屏开启移动数据网络
     * 副屏需要配合执行 openSubScreenDhcp()
     *
     * @return
     */
    public static boolean openMainScreenDataNetwork() {

        // 开启移动数据网络
        boolean svcDataEnable = SystemCtrlUtil.RootCommand("svc data enable");
        Log.d("command", "RootCommand svcDataEnable 操作状态: " + svcDataEnable);

        // 拉起 eth0 dhcp
        boolean netcfgEth0Down = SystemCtrlUtil.RootCommand("netcfg eth0 down");
        Log.d("command", "RootCommand netcfgEth0Down 操作状态: " + netcfgEth0Down);

        boolean netcfgEth0Up = SystemCtrlUtil.RootCommand("netcfg eth0 up");
        Log.d("command", "RootCommand netcfgEth0Up 操作状态: " + netcfgEth0Up);

        if (svcDataEnable && netcfgEth0Down && netcfgEth0Up) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 主屏关闭移动数据网络
     * 副屏需要配合执行 openSubScreenDhcp()
     *
     * @return
     */
    public static boolean closeMainScreenDataNetwork() {
        // 关闭移动数据网络
        boolean svcDataEnable = SystemCtrlUtil.RootCommand("svc data disable");
        Log.d("command", "RootCommand svcDataEnable 操作状态: " + svcDataEnable);

        // 拉起 eth0 dhcp
        boolean netcfgEth0Down = SystemCtrlUtil.RootCommand("netcfg eth0 down");
        Log.d("command", "RootCommand netcfgEth0Down 操作状态: " + netcfgEth0Down);

        boolean netcfgEth0Up = SystemCtrlUtil.RootCommand("netcfg eth0 up");
        Log.d("command", "RootCommand netcfgEth0Up 操作状态: " + netcfgEth0Up);

        if (svcDataEnable && netcfgEth0Down && netcfgEth0Up) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 副屏拉起dhcp
     *
     * @return
     */
    public static boolean openSubScreenDhcp() {

        // 拉起 eth0 dhcp
        boolean netcfgEth0Down = SystemCtrlUtil.RootCommand("netcfg eth0 down");
        Log.d("command", "RootCommand netcfgEth0Down 操作状态: " + netcfgEth0Down);

        boolean netcfgEth0Up = SystemCtrlUtil.RootCommand("netcfg eth0 up");
        Log.d("command", "RootCommand netcfgEth0Up 操作状态: " + netcfgEth0Up);

        if (netcfgEth0Down && netcfgEth0Up) {
            return true;
        } else {
            return false;
        }

    }

    public static String getEthIpAddress() {
        String infaceName = "eth0";
        String ip = "0.0.0.0";
        try {
            Enumeration<NetworkInterface> netInterface = NetworkInterface.getNetworkInterfaces();
            while (netInterface.hasMoreElements()) {
                NetworkInterface inface = netInterface.nextElement();
                if (!inface.isUp()) {
                    continue;
                }

                // eth0 有线网络判断
                if (!infaceName.equals(inface.getDisplayName())) {
                    continue;
                }

                Enumeration<InetAddress> netAddressList = inface.getInetAddresses();
                while (netAddressList.hasMoreElements()) {
                    InetAddress inetAddress = netAddressList.nextElement();
                    // 获取IP4地址
                    if (inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {

        }
        return ip;
    }

//    public class DhcpStaticIpSetter {
//        public  void setStaticIp(String ipAddress, String gateway, String dnsServer) {
//            try {
//                // 设置IP地址
//                SystemProperties.set("dhcp.eth0.ipaddress", ipAddress);
//                // 设置网关
//                SystemProperties.set("dhcp.eth0.gateway", gateway);
//                // 设置DNS服务器
//                SystemProperties.set("dhcp.eth0.dns1", dnsServer);
//
//                // 重启网络接口
//                Runtime.getRuntime().exec("ifconfig eth0 down");
//                Runtime.getRuntime().exec("ifconfig eth0 up");
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
//   echo "static ip_address=$STATIC_IP" >> /data/misc/dhcp/dhcpcd_eth0.conf

//    public static void getEthernetIp(Context context) {
//        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//        if (connectivityManager != null) {
//            Network[] networks = connectivityManager.getAllNetworks();
//            for (Network network : networks) {
//                NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network);
//                if (networkCapabilities != null) {
//                    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
//                        Log.d(TAG, "Ethernet connection found");
//                        // 获取IP地址
//                        String ipAddress = getIpAddress(connectivityManager, network);
//                        if (ipAddress != null) {
//                            Log.d(TAG, "IP Address: " + ipAddress);
//                        } else {
//                            Log.d(TAG, "No IP address found");
//                        }
//                    }
//                }
//            }
//        } else {
//            Log.e(TAG, "ConnectivityManager is null");
//        }
//    }
//
//    private static String getIpAddress(ConnectivityManager connectivityManager, Network network) {
//        try {
//            LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
//            if (linkProperties != null) {
//                for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) {
//                    return linkAddress.getAddress().getHostAddress(); // 返回IP地址字符串
//                }
//            }
//        } catch (Exception e) {
//            Log.e(TAG, "Error getting IP address", e);
//        }
//        return null;
//    }

}
